package top.yifengxiner.blockchain.controller;

import com.github.binarywang.utils.qrcode.QrcodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yifengxiner.blockchain.pojo.entity.Academic;
import top.yifengxiner.blockchain.pojo.entity.Course;
import top.yifengxiner.blockchain.pojo.entity.Diploma;
import top.yifengxiner.blockchain.pojo.vo.EducatedVo;
import top.yifengxiner.blockchain.pojo.vo.IdVo;
import top.yifengxiner.blockchain.pojo.vo.StringVo;
import top.yifengxiner.blockchain.service.AcademicService;
import top.yifengxiner.blockchain.service.CourseService;
import top.yifengxiner.blockchain.service.DiplomaService;
import top.yifengxiner.blockchain.service.EducatedService;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.common.utils.TokenUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/11 18:54
 * @description：受教育者
 * @modified By：
 * @version: 0.0.1$
 */
@Api("受教育者模块")
@RestController
@RequestMapping("/blockchain")
public class EducatedController
{

    @Autowired
    private EducatedService educatedService;

    @Autowired
    private AcademicService academicService;

    @Autowired
    private DiplomaService diplomaService;

    @Autowired
    private CourseService courseService;

    @ApiOperation("通过受教育者ID查询学历证书列表")
    @PostMapping("/educated/get_academic")
    public AjaxResult getAcademic(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Academic> list = academicService.getAcademic(idVo);
        return AjaxResult.success(list);
    }


    /**
     * 通过学历证书ID获得学历证书
     * @return
     */
    @ApiOperation("通过学历证书ID获得学历证书")
    @PostMapping("/educated/get_academic_info")
    public AjaxResult getAcademicInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        Academic academic = academicService.getAcademicInfo(idVo);
        return AjaxResult.success(academic);
    }


    @ApiOperation("通过受教育者ID查询学位证书列表")
    @PostMapping("/educated/get_diploma")
    public AjaxResult getDiploma(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Diploma> list = diplomaService.getDiploma(idVo);
        return AjaxResult.success(list);
    }

    /**
     * 通过学位证书ID获得学位证书
     * @return
     */
    @ApiOperation("通过学位证书ID获得学位证书")
    @PostMapping("/educated/get_diploma_info")
    public AjaxResult getDiplomaInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        Diploma diploma = diplomaService.getDiplomaInfo(idVo);
        return AjaxResult.success(diploma);
    }

    /**
     * 获得课程信息
     * @return
     */
    @ApiOperation("通过学历证书ID获得课程信息")
    @PostMapping("/educated/get_course_info")
    public AjaxResult getCourseInfo(@RequestBody IdVo idVo)
    {
        if(idVo.getId()==null)
        {
            return AjaxResult.error("id为空");
        }
        List<Course> list = courseService.getCourse(idVo);
        return AjaxResult.success(list);
    }

    @ApiOperation("通过受教育者ID修改个人信息")
    @PostMapping("/educated/update_educated")
    public AjaxResult updateEducator(@RequestBody EducatedVo educatedVo)
    {
        return educatedService.updateEducated(educatedVo);
    }

    @ApiOperation("获取二维码接口")
    @GetMapping("/ScanCode")
    public  void getScanCode(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
        response.setHeader("token",request.getHeader("token"));
        String id = request.getParameter("educatedId");
        System.out.println(id);
        response.setContentType("image/jpg");
        String url = "http://192.168.1.106:8080/#/code/educated?educatedId=" + id;
        System.out.println(url);
        byte[] bytes = QrcodeUtils.createQrcode(url,400,null);
        OutputStream stream = response.getOutputStream();
        stream.write(bytes);
    }

    @ApiOperation("核验证书二维码接口")
    @GetMapping("/CheckCode")
    public void checkCode(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
        response.setHeader("token",request.getHeader("token"));
        String id;
        String url;
        String academicId = request.getParameter("academicId");
        String diplomaId = request.getParameter("diplomaId");
        if(academicId == null)
        {
            id = diplomaId;
            url = "http://192.168.1.106:8080/#/code/check?diplomaId=" + id;
        }
        else
        {
            id = academicId;
            url = "http://192.168.1.106:8080/#/code/check?academicId=" + id;
        }
        System.out.println(id);
        response.setContentType("image/jpg");
        System.out.println(url);
        byte[] bytes = QrcodeUtils.createQrcode(url,400,null);
        OutputStream stream = response.getOutputStream();
        stream.write(bytes);
    }

    @ApiOperation("核验证书")
    @GetMapping("/CheckAcademic")
    public AjaxResult checkAcademic(HttpServletRequest request,HttpServletResponse response)
    {
        response.setHeader("token",request.getHeader("token"));
        String academicId = request.getParameter("academicId");
        String diplomaId = request.getParameter("diplomaId");
        return AjaxResult.success();
        /*if(academicId == null)
        {
            return diplomaService.checkDiploma(diplomaId);
        }
        return academicService.checkAcademic(academicId);*/
    }

    @ApiOperation("通过token获取Educated")
    @PostMapping("/educator/get_educated_token")
    public AjaxResult getEducator(@RequestBody StringVo stringVo)
    {
        String EducatedName = TokenUtils.getSubject(stringVo.getString());
        return AjaxResult.success(educatedService.getEducatedByName(EducatedName));
    }

}
